package com.thyme.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.thyme.dto.User;
import com.thyme.dto.UserQueryCondition;
import com.thyme.exception.UserNotExistException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import sun.plugin.liveconnect.SecurityContextHelper;


import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ah.caixiaodong@aisino.com
 * @ClassName UserController
 * @Description TODO
 * @Date 2019/6/3 21:09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/me")
    public Object getCurrentUser(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult erros){

        if (erros.hasErrors()){
            erros.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
        }

        logger.info(user.getId());
        logger.info(user.getUsername());
        logger.info(user.getPassword());
        logger.info(user.getBirthday().toString());
        user.setId("1");
        return user;
    }

    @PutMapping("/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult erros){

        if (erros.hasErrors()){
            erros.getAllErrors().stream().forEach(error -> {
                /*FieldError fieldError = (FieldError) error;
                String message = fieldError.getField() + " " +error.getDefaultMessage();*/
                System.out.println(error.getDefaultMessage());
            });
        }

        logger.info(user.getId());
        logger.info(user.getUsername());
        logger.info(user.getPassword());
        logger.info(user.getBirthday().toString());
        user.setId("1");
        return user;
    }

    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable String id){
        System.out.println(id);
    }

    //@RequestMapping(value = "/user",method = RequestMethod.GET)
    @GetMapping()
    @JsonView(User.UserSimpleView.class)
    @ApiOperation(value = "用户查询服务")
    public List<User> query(UserQueryCondition userQueryCondition, Pageable pageable){
        System.out.println(ReflectionToStringBuilder.toString(userQueryCondition, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getSort());
        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }

    //@RequestMapping(value = "/user/{id:\\d+}",method = RequestMethod.GET)
    @GetMapping("/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    public User getInfo(@ApiParam("用户Id") @PathVariable String id){

        //throw new UserNotExistException(id);
        logger.info("进入getInfo服务");
        User user = new User();
        user.setUsername("tom");
        return user;
    }

}
